package ${domain}.module.${moduleName}.mgr;

import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.Message;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.auth.Token;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.${moduleName}.ent.${table.getCName()};
import ${domain}.module.${moduleName}.mpr.${table.getCName()}Mapper;
<#if table.getCreate()>
import ${domain}.module.${moduleName}.req.${table.getCName()}CreateRequest;
</#if>
<#if table.getDelete()>
import ${domain}.module.${moduleName}.req.${table.getCName()}DeleteRequest;
</#if>
<#if table.getFind()>
import ${domain}.module.${moduleName}.req.${table.getCName()}FindRequest;
</#if>
<#if table.getGet()>
import ${domain}.module.${moduleName}.req.${table.getCName()}GetRequest;
</#if>
<#if table.getSearch()>
import ${domain}.module.${moduleName}.req.${table.getCName()}SearchRequest;
</#if>
<#if table.getUpdate()>
import ${domain}.module.${moduleName}.req.${table.getCName()}UpdateRequest;
</#if>
<#if table.getCreate()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}CreateResponse;
</#if>
<#if table.getDelete()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}DeleteResponse;
</#if>
<#if table.getFind()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}FindResponse;
</#if>
<#if table.getGet()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}GetResponse;
</#if>
<#if table.getSearch()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}SearchResponse;
</#if>
<#if table.getUpdate()>
import ${domain}.module.${moduleName}.rsp.${table.getCName()}UpdateResponse;
</#if>
<#list table.methods as item>
<#if item.selected>
import ${domain}.module.${moduleName}.req.${table.getCName()}${item.getAbbName()?default("")}Request;
import ${domain}.module.${moduleName}.rsp.${table.getCName()}${item.getAbbName()?default("")}Response;
</#if>
</#list>
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ${table.tableName} - ${table.tableComment}
 *
 * @author ${author?default("")}
 * @version 0.0.1
 * @since ${.now?string["yyyy-MM-dd"]}
 */
@Transactional
@Service
public class ${table.getCName()}ManagerImpl implements ${table.getCName()}Manager {

    @Autowired
    private ${table.getCName()}Mapper ${table.getFName()}Mapper;
<#if table.getCreate()>

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}CreateResponse create(${table.getCName()}CreateRequest request, Token token) {
        ${table.getCName()}CreateResponse response = new ${table.getCName()}CreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }
<#list table.fields as field>
<#if field.isUnique>

        {// ${field.fieldComment}唯一检查
            ${table.getCName()}FindRequest ${table.getFName()}FindRequest = new ${table.getCName()}FindRequest();
            ${table.getFName()}FindRequest.${field.setterName()}(request.${field.getterName()}());
            ${table.getCName()}FindResponse ${table.getFName()}FindResponse = this.find(${table.getFName()}FindRequest, token);
            if (${table.getFName()}FindResponse.hasError()) {
                response.addErrors(${table.getFName()}FindResponse.getErrors());
                return response;
            } else if (${table.getFName()}FindResponse.getTotalCount() > 0) {
                response.addError(ErrorType.UNIQUENESS_ERROR, "[${field.getFName()}]${field.fieldComment}已存在，请检查!");
                return response;
            }
        }
</#if>
</#list>

        <#if table.sys>
        long id = IDgenerator.nextId();
        ${table.getCName()} entity = MapperUtil.map(request, ${table.getCName()}.class);
        entity.setId(id);
        <#else>
        ${table.getCName()} entity = MapperUtil.map(request, ${table.getCName()}.class);
        </#if>

        long result = ${table.getFName()}Mapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        <#if table.sys>
        response.setId(id);
        </#if>

        return response;
    }
</#if>
<#if table.getDelete()>

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}DeleteResponse delete(${table.getCName()}DeleteRequest request, Token token) {
        ${table.getCName()}DeleteResponse response = new ${table.getCName()}DeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = ${table.getFName()}Mapper.delete(request.getId(), token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }
</#if>
<#if table.getUpdate()>

    /**
     * 更新
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public ${table.getCName()}UpdateResponse update(${table.getCName()}UpdateRequest request, Token token) {
        ${table.getCName()}UpdateResponse response = new ${table.getCName()}UpdateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        ${table.getCName()} entity = ${table.getFName()}Mapper.getById(request.getId(), token);
        if (entity == null) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
            return response;
        }

        MapperUtil.map(request, entity);
        long result = ${table.getFName()}Mapper.update(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.UPDATE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }
</#if>
<#if table.getFind()>

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public ${table.getCName()}FindResponse find(${table.getCName()}FindRequest request, Token token) {
        ${table.getCName()}FindResponse response = new ${table.getCName()}FindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<${table.getCName()}> pageInfo = new PageInfo<>(${table.getFName()}Mapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }
</#if>
<#if table.getGet()>

    /**
     * 获得对象
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public ${table.getCName()}GetResponse get(${table.getCName()}GetRequest request, Token token) {
        ${table.getCName()}GetResponse response = new ${table.getCName()}GetResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        ${table.getCName()} po = ${table.getFName()}Mapper.getById(request.getId(), token);

        if (po != null) {
            response.set${table.getCName()}(po);
        } else {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
        }

        return response;
    }
</#if>
<#if table.getSearch()>

    /**
     * 模糊查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public ${table.getCName()}SearchResponse search(${table.getCName()}SearchRequest request, Token token) {
	    ${table.getCName()}SearchResponse response = new ${table.getCName()}SearchResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<${table.getCName()}> pageInfo = new PageInfo<>(${table.getFName()}Mapper.search(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }
</#if>
<#list table.methods as item>
<#if item.selected>

    /**
     * ${item.note?default("")}
     *
     * @param request 请求对象
     * @param token   令牌
     * @return
     */
    public ${table.getCName()}${item.getAbbName()?default("")}Response ${item.name}(${table.getCName()}${item.getAbbName()?default("")}Request request, Token token) {
        ${table.getCName()}${item.getAbbName()?default("")}Response response = new ${table.getCName()}${item.getAbbName()?default("")}Response();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        return response;
    }
</#if>
</#list>
}
